package work.sby.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import work.sby.mapper.BrandMapper;
import work.sby.mapper.BuyMapper;

import work.sby.pojo.Brand;
import work.sby.pojo.Buy;
import work.sby.util.SqlSessionFactoryUtils;

public class BuyService {
    //调用BrandMapper的selectAll方法，返回所有品牌信息

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


   public List<Buy> selectBuy(){

//查询所有


//2.创建SqlSession对象
           SqlSession sqlSession = factory.openSession();

//获取brandmapper
           BuyMapper mapper = sqlSession.getMapper(BuyMapper.class);
//        3.调用方法
           List<Buy> buys = mapper.selectBuy();
//        4.关闭SqlSession
           sqlSession.close();
           return buys;
       }
    public List<Buy> selectByBuyName(String buyername){

    //查询所有


    //2.创建SqlSession对象
        SqlSession sqlSession = factory.openSession();

    //获取brandmapper
        BuyMapper mapper = sqlSession.getMapper(BuyMapper.class);
    //        3.调用方法
        List<Buy> buys = mapper.selectByBuyName(buyername);
    //        4.关闭SqlSession
        sqlSession.close();
        return buys;
    }
    public void updatePayValue(int id,String buyername){
        //查询所有


        //2.创建SqlSession对象
        SqlSession sqlSession = factory.openSession();

        //获取brandmapper
        BuyMapper mapper = sqlSession.getMapper(BuyMapper.class);
        //        3.调用方法
        mapper.updatePayValue(id,buyername);
        //        4.关闭SqlSession
        sqlSession.commit();
        sqlSession.close();

    }
    public void deleteByIdAndBuyName(int id,String buyername)
    {
        SqlSession sqlSession = factory.openSession();

        //获取brandmapper
        BuyMapper mapper = sqlSession.getMapper(BuyMapper.class);
        //        3.调用方法
        mapper.deleteByIdAndBuyName(id,buyername);
        //        4.关闭SqlSession
        sqlSession.commit();
        sqlSession.close();
    }
    public void addbuy(String buyername,String goodname)
    {
        SqlSession sqlSession = factory.openSession();

        //获取brandmapper
        BuyMapper mapper = sqlSession.getMapper(BuyMapper.class);
        //        3.调用方法
        mapper.addbuy(buyername,goodname);
        //        4.关闭SqlSession
        sqlSession.commit();
        sqlSession.close();
    }

   }
